package com.aiiju.core.cont;

import java.io.InputStream;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aiiju.core.util.StringTool;
import com.aiiju.core.util.UrlUtils;
import com.aiiju.core.util.txtParse;
import com.bole.resumeparser.models.TextResumeData;
import com.tongdainfo.commom.util.FileToString;

@Controller
@RequestMapping(value="/url")
public class UrlController {
	
	/**
	 * 查看简历的详情
	 * @Description:
	 * @param fileUrl
	 * @param model
	 * @return
	 * @throws Exception
	 * @author: aiju
	 * @time:2017年11月22日 下午4:35:09
	 */
	@RequestMapping(value="/info.do")
	public String urlPage(String fileUrl,Model model) throws Exception {
		
		// url 转成文本
		String urlContext = this.getUrlContext(fileUrl);
		boolean hasContent = StringUtils.isNotBlank(urlContext);
		if(hasContent) {
			model.addAttribute("fileContext",urlContext);
			
			txtParse txtParse = new txtParse();
			String parseTxt = txtParse.parseTxt(urlContext);
			model.addAttribute("jsonContext",parseTxt);
		}
		return "url";
	}
	
	/**
	 * 查看简历的详细信息
	 * @Description:
	 * @param fileUrl 文件的url路径
	 * @param model
	 * @return
	 * @throws Exception
	 * @author: aiju
	 * @time:2017年11月22日 下午4:35:27
	 */
	@RequestMapping(value="/detail.do")
	public String urlPageDetail(String fileUrl,Model model) throws Exception {
		// url 转成文本
		String urlContext = this.getUrlContext(fileUrl);
		boolean hasContent = StringUtils.isNotBlank(urlContext);
		if(hasContent) {
			model.addAttribute("fileContext",urlContext);
			
			// 转成对象返回给前端
			txtParse txtParse = new txtParse();
//			String parseTxt = txtParse.parseTxt(urlContext);
			TextResumeData textResume = txtParse.parseObj(urlContext);
			model.addAttribute("resume",textResume);
		}
		return "urlDetail";
	}
	
	public  String getUrlContext(String url) throws Exception {
		
		boolean netFileAvailable = UrlUtils.isNetFileAvailable(url);
		if(netFileAvailable) {
			// Io 流
			InputStream urlInputStream = UrlUtils.urlToIO(url);
			FileToString fileToString = new FileToString();
			// 文件名
			String filename = StringTool.getFileName(url);
			String fileContent = fileToString.getFileContent(filename, urlInputStream);
			
			return fileContent;
		}
		return null;
	}

}
